package hsp.admin.app.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.system.po.ParamPO;
import hsp.admin.app.system.service.IParamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "ParamController", description = "参数管理")
@RestController
@RequestMapping("system/param")
public class ParamController {

    private final IParamService service;

    @Autowired
    public ParamController(IParamService service){
        this.service = service;
    }

    @Operation(summary = "获取参数")
    @GetMapping("{id}")
    public ApiResult<ParamPO> getParam(@PathVariable("id") long id){
        ParamPO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "获取参数分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<ParamPO>> getParamPage(@PathVariable("page")int page, @PathVariable("size")int size, ParamPO po){
        LambdaQueryWrapper<ParamPO> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(po.getCode())) wrapper.like(ParamPO::getCode, po.getCode());
        if(StringUtils.isNotBlank(po.getName())) wrapper.like(ParamPO::getName, po.getName());
        if(StringUtils.isNotBlank(po.getValue())) wrapper.like(ParamPO::getValue, po.getValue());
        if(StringUtils.isNotBlank(po.getStatus())) wrapper.eq(ParamPO::getStatus, po.getStatus());
        Page<ParamPO> records = service.page(new Page<>(page,size),wrapper);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑参数")
    @PostMapping()
    public ApiResult<?> postOrPutParam(@RequestBody ParamPO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除参数")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteParam(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
